﻿/// <reference path="../../mapparola.client/scripts/typings/knockout/knockout.d.ts" />

module Mapparola.Core.Client {

    export class DiscussionC extends Core.Discussion {
        
        private OText: KnockoutObservable<string>;
        public OFavorited: KnockoutObservable<boolean>;
        public OResponses: KnockoutObservableArray<Client.DiscussionC>;

        constructor() {
            super();
            this.OText = ko.observable<string>();
            this.OFavorited = ko.observable<boolean>();

            this.OResponses = ko.observableArray<Client.DiscussionC>();
            
        }

        public SetText(txt: string): void {
            super.SetText(txt);
            this.OText = ko.observable(txt);
        }

        public Favorite(): void {

            this.OFavorited(true);
            this.IsPersonal = true;
        }

        public static FromServerObject(messageJson: any): DiscussionC {
            var discussion: DiscussionC = new DiscussionC();

            discussion.Token = messageJson.Token;
            
            discussion.SetText(messageJson._text);
            discussion.Location = new Mapparola.Core.Point(messageJson.Location.y, messageJson.Location.x);
            discussion.IsPersonal = messageJson.IsPersonal;
            discussion.CreatedDate = messageJson.CreatedDate;
            
            //this is not really recursive, we support only 1 level of nested replies
            var responses: DiscussionC[] = null, response: DiscussionC;
            if (messageJson.Responses && messageJson.Responses.length) {
                responses = [];

                (<any[]>messageJson.Responses).forEach((response: any) => {
                    response = Core.Client.DiscussionC.FromServerObject(response);

                    discussion.OResponses.push(response)
                    responses.push(response);
                });
            }
            
            return discussion;
        }
    }
}